package org.infodavid.common.persistence;

import java.util.List;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.infodavid.common.model.IUserConnection;
import org.infodavid.common.model.criteria.ISearchCriteria;
import org.infodavid.common.persistence.impl.AbstractDataService;
import org.infodavid.common.reflect.InstantiationHelper;

/**
 * The Class UserConnectionDataService.
 */
public abstract class UserConnectionDataService extends AbstractDataService<IUserConnection,Long> {

  /** The singleton. */
  private static final UserConnectionDataService SINGLETON;

  /** The Constant log. */
  private static final Log LOGGER = LogFactory.getLog(UserConnectionDataService.class);

  static {
    try {
      SINGLETON = InstantiationHelper.getInstance().newInstance(UserConnectionDataService.class);
    }
    catch (final Exception e) {
      LOGGER.fatal("Can not instantiate the " + UserConnectionDataService.class.getName(), e);

      throw new Error(e);
    }
  }

  /**
   * returns the singleton.
   * @return the singletons
   */
  public static UserConnectionDataService getInstance() {
    return SINGLETON;
  }

  /**
   * Instantiates a new service.
   */
  protected UserConnectionDataService() {
    super();
  }

  /**
   * Find the active connections of a user.
   * @param session the session
   * @param userKey the user key
   * @param results the results
   * @param criteria the criteria
   * @return the number of results
   * @throws PersistenceException the persistence exception
   */
  public abstract long findConnectedByUser(PersistenceSession session, Long userKey,
      List<IUserConnection> results, ISearchCriteria criteria) throws PersistenceException;

  /**
   * Checks if is connection of.
   * @param persistenceSession the persistence session
   * @param connectionKey the connection key
   * @param userKey the user key
   * @return true, if is connection of
   * @throws PersistenceException the persistence exception
   */
  public abstract boolean isConnectionOf(PersistenceSession persistenceSession, Long connectionKey,
      Long userKey) throws PersistenceException;
}
